import numpy as np
import sympy
import matplotlib.pyplot as plt

x = sympy.Symbol('x')
exp = np.e**x

def polyApprox(func,num_terms):
    # 当我们需要反复做类似的步骤的时候，最好将步骤定义为一个函数
    sums = 0
    for i in range(num_terms):
        numerator = func.diff(x,i)
        numerator = numerator.evalf(subs={x:0})
        denominator = np.math.factorial(i)
        sums += numerator/denominator*x**i
    return sums
    
sum5 = polyApprox(exp,5)
sum10 = polyApprox(exp,10)
sum15 = exp.series(x,0,15).removeO()

xvals = np.linspace(5,10,100)
for xval in xvals:
    plt.plot(xval,exp.evalf(subs={x:xval}),'bo'
            , xval,sum5.evalf(subs={x:xval}),'ro'
            , xval,sum10.evalf(subs={x:xval}),'go'
            , xval,sum15.evalf(subs={x:xval}),'yo')

plt.show()